const App = getApp()

Page({
  data: {
    logged: !1,
    xian: false
  },
  onLoad(obj) {
 

    console.log(obj)
    if (obj) {
      this.setData({
        uis: obj
      })
    }
  },

  onShow() {
  },
  login() {
    this.signIn(this.goIndex)
  },
  goIndex() {
    console.log('toindex')
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  getUserInfo(res) {
    console.log("Res",res)
    let d = res.detail.userInfo
    wx.setStorageSync('ziji', d)
    if (res.detail.errMsg == "getUserInfo:ok") {
      let res_user = res.detail
      let userInfo = res_user.userInfo
      let openId = wx.getStorageSync('openid')
      userInfo.openId = openId
      //是否邀请
      userInfo.isNew = wx.getStorageSync('dengl')
      // 活动ID
      userInfo.inviteId = this.data.uis.did
      //邀请人ID
      userInfo.inviteBuyerId = this.data.uis.uid
    
      App.HttpService.saveUser(userInfo).then(res => {
        console.log('saveUse55555555555555555r', res)
        if (res.data.code == 200) {
          this.setData({
            xian: true
          })
          wx.setStorageSync('dengl', "1")
        }
      }, error => {
        console.log(error, "error")
      })
      // wx.navigateBack({ // 返回首页
      // 	delta: 1
      // })
    } else {
      wx.showModal({
        title: '警告通知',
        content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
        success: function(res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
                if (res.authSetting["scope.userInfo"]) { // 如果用户重新同意了授权登录
                  wx.getUserInfo({
                    withCredentials: true,
                    success: function(res_user) {
                      let userInfo = res_user.userInfo
                      let openId = wx.getStorageSync('openid')
                      userInfo.openId = openId
                      console.log(res_user,"res_userres_user")
                      App.HttpService.saveUser(userInfo).then(res => {

                      }, error => {
                        console.log(error)
                      })
                    }
                  })

                  wx.navigateBack({ // 返回首页
                    delta: 1
                  })
                }
              },
              fail: function(res) {}
            })
          }
        }
      })
    }
  },
  showModal() {
    App.WxService.showModal({
      title: '友情提示',
      content: '获取用户登录状态失败，请重新登录',
      showCancel: !1,
    })
  },
  signIn(cb) {
    if (App.WxService.getStorageSync('token')) return
    App.HttpService.signIn({
      username: 'admin',
      password: '123456',
    }).then(res => {
      const data = res.data
      console.log(data)
      if (data.meta.code == 0) {
        App.WxService.setStorageSync('token', data.data.token)
        cb()
      }
    })
  },
  getPhoneNumber: function(e) {
    var _this = this
    console.log(e)
    wx.getUserInfo({
      success(res) {
        console.log(res, "632515485")
        let userParams = {
          "encryptedData": e.detail.encryptedData,
          "rawData": res.rawData,
          "signature": res.signature,
          "iv": e.detail.iv,
          "sessionKey": wx.getStorageSync('sessionKey'),
          "openid": wx.getStorageSync('openid'),
        }
        App.HttpService.checkUserInfo(userParams).then(res => {

          console.log(res, "手机号的返回");
          var yao = _this.data.uis.yao
    
          if (yao) {
            wx.navigateTo({
              url: '/pages/fenxiangym/index',
            })
            return
          }
       

          if (res.data.code == 200) {

            wx.showToast({
              title: '获取手机号成功',
              icon: 'success',
              duration: 2000
            })

            var u = _this.data.uis.uid
            if (u) {
              let d = 0
              wx.getStorageSync('dengl');
              console.log("66666我的大电盾科技66666", d)
              if (d == 1) {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              } else {
                let defaultCar = wx.getStorageSync('defaultCar')
                if (defaultCar) {

                  wx.navigateTo({
                    url: '/pages/product/package/index',
                  })
                } else {
                  wx.navigateTo({
                    url: '/pages/car/firstMenu/index',
                  })
                }
              }
            } else {
              wx.navigateBack({ // 返回首页
                delta: 1
              })
            }
          } else {
            console.log("获取手机号失败，请重试")
            wx.showToast({
              title: '手机号获取失败',
              icon: 'none',
              duration: 1500
            })
            var u = _this.data.uis.uid

            if (u) {
              let d = 0
              wx.getStorageSync('dengl');
              console.log("66666我的大电盾科技66666", d)
              if (d == 1) {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              } else {
                let defaultCar = wx.getStorageSync('defaultCar')
                if (defaultCar) {

                  wx.navigateTo({
                    url: '/pages/product/package/index',
                  })
                } else {
                  wx.navigateTo({
                    url: '/pages/car/firstMenu/index',
                  })

                }





              }
            } else {
              wx.navigateBack({ // 返回首页
                delta: 1
              })
            }
          }

        })


      }

    })

  },

  jujue: function() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

})